package cn.springcloud.fix.common.core.taobao.api;

import cn.springcloud.fix.common.core.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TaobaoHashMap extends HashMap<String, String> {

    private static final long serialVersionUID = -1277791390393392630L;

    public TaobaoHashMap() {
        super();
    }

    public TaobaoHashMap(Map<? extends String, ? extends String> m) {
        super(m);
    }

    public String put(String key, Object value) {
        String strValue;

        if (value == null) {
            strValue = null;
        } else if (value instanceof String) {
            strValue = (String) value;
        } else if (value instanceof Integer) {
            strValue = value.toString();
        } else if (value instanceof Long) {
            strValue = value.toString();
        } else if (value instanceof Float) {
            strValue = value.toString();
        } else if (value instanceof Double) {
            strValue = value.toString();
        } else if (value instanceof Boolean) {
            strValue = value.toString();
        } else if (value instanceof Date) {
            strValue = DateUtil.formatDate((Date) value);
        } else {
            strValue = value.toString();
        }

        return this.put(key, strValue);
    }

    public String put(String key, String value) {
        if (StringUtils.isNoneEmpty(key, value)) {
            return super.put(key, value);
        } else {
            return null;
        }
    }
}
